package com.demo.service.impl;


import java.util.List;

import com.demo.entity.enums.CategoryTypeEnum;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.Category;

import com.demo.entity.query.CategoryQuery;

import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.service.CategoryService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.CategoryMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:分类serviceImpl
 * @Date:2025-03-02
 * @author：author
*/
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService{

	@Resource
	private CategoryMapper<Category,CategoryQuery> categoryMapper;

	/**
	 * 根据条件查询列表
	 */
	public List<Category> findListByParam(CategoryQuery query) { 
		return this.categoryMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(CategoryQuery query){
		return this.categoryMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<Category> findListByPage(CategoryQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<Category> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(Category bean) { 
		return this.categoryMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<Category> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.categoryMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<Category> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.categoryMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据CategoryId查询
	 */
	public Category getCategoryByCategoryId(Integer categoryId) { 
		return this.categoryMapper.selectByCategoryId(categoryId);
	}

	/**
	 * 根据CategoryId更新
	 */
	public Integer updateCategoryByCategoryId(Category bean, Integer categoryId) { 
		return this.categoryMapper.updateByCategoryId(bean,categoryId);
	}

	/**
	 * 根据CategoryId删除
	 */
	public Integer deleteCategoryByCategoryId(Integer categoryId) { 
		return this.categoryMapper.deleteByCategoryId(categoryId);
	}

	/**
	 * 保存分类信息
	 * @param category
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveCategory(Category category) {
		if (null == category.getCategoryId()) {
			CategoryQuery categoryQuery = new CategoryQuery();
			Integer count = this.categoryMapper.selectCount(categoryQuery);
			category.setSort(count + 1);
			this.categoryMapper.insert(category);
		} else {
			this.categoryMapper.updateByCategoryId(category, category.getCategoryId());
		}

		CategoryQuery categoryQuery = new CategoryQuery();
		categoryQuery.setCategoryName(category.getCategoryName());
		categoryQuery.setType(category.getType());
		Integer count = this.categoryMapper.selectCount(categoryQuery);
		if (count > 1) {
			throw new BusinessException("分类名称重复");
		}
		if (null == category.getCategoryName()) {
			return;
		}
		Category dbInfo = categoryMapper.selectByCategoryId(category.getCategoryId());
		if (!dbInfo.getCategoryName().equals(category.getCategoryName())) {
			categoryMapper.updateCategoryName(category.getCategoryName(), category.getCategoryId());
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void changeSort(String categoryIds) {
		String[] categoryIdArray = categoryIds.split(",");
		Integer index = 1;
		for (String categoryIdStr : categoryIdArray) {
			Integer cateoryId = Integer.parseInt(categoryIdStr);
			Category category = new Category();
			category.setSort(index);
			categoryMapper.updateByCategoryId(category, cateoryId);
			index++;
		}
	}

	/**
	 * 查询所有的分类
	 * @param type
	 * @return
	 */
	@Override
	public List<Category> loadAllCategoryByType(Integer type) {
		CategoryTypeEnum typeEnum = CategoryTypeEnum.getByType(type);
		if (typeEnum == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		CategoryQuery categoryQuery = new CategoryQuery();
		categoryQuery.setOrderBy("sort asc");
		categoryQuery.setTypes(new Integer[]{typeEnum.getType(), CategoryTypeEnum.POST.getType()});
		return this.categoryMapper.selectList(categoryQuery);
	}

}